package com.zhiyou.service.SystemLogisticsServiceImpl;



import com.zhiyou.bean.Logistics;
import com.zhiyou.bean.LogisticsExample;
import com.zhiyou.dao.LogisticsMapper;
import com.zhiyou.service.SystemLogisticsService.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class LogisticsServiceImpl implements LogisticsService {

    @Autowired
    LogisticsMapper logisticsMapper;

    public List<Logistics> getAll(){
        LogisticsExample ex = new LogisticsExample();
        LogisticsExample.Criteria criteria = ex.createCriteria();
        criteria.andStateTo();
        List<Logistics> list = logisticsMapper.selectByExample(ex);
        return list;
    }

    @Override
    public Logistics detail(Integer id) {
        System.out.println(id);
        Logistics logistics = logisticsMapper.selectByPrimaryKey(id);
        return logistics;
    }

    @Override
    public int update(Logistics logistics) {
        logisticsMapper.updateByPrimaryKeySelective(logistics);
        return 1;
    }

    @Override
    public int addOne(Logistics logistics) {
        int i = logisticsMapper.insert(logistics);
        return i;
    }

    @Override
    public int deleteOne(int id) {
        Logistics logistics = logisticsMapper.selectByPrimaryKey(id);
        System.out.println(logistics);
        logistics.setLoState(1);
        System.out.println("logistics = " + logistics);
        logisticsMapper.updateByPrimaryKey(logistics);
        return 1;
    }

    @Override
    public List<Logistics> search(String field, String keyword) {
        System.out.println("===============1============");
        LogisticsExample ex = new LogisticsExample();
        LogisticsExample.Criteria criteria = ex.createCriteria();
        criteria.andStateTo();
        List<Logistics> logistics = new ArrayList<>();
        System.out.println("===============2============");

        String str= "";
        if ("loId".equals(field)){
            str = keyword;
        }else {
            str = "%"+keyword+"%";
        }
        if ("loId".equals(field)){
            Logistics logistics1 = logisticsMapper.selectByPrimaryKey(Integer.parseInt(str));
            logistics.add(logistics1);
        }else if ("loName".equals(field)){
            criteria.andLoNameLike(str);
            logistics = logisticsMapper.selectByExample(ex);
        }else if ("loTel".equals(field)){
            criteria.andLoTelLike(str);
            logistics = logisticsMapper.selectByExample(ex);
        }else if ("loIdcard".equals(field)){
            criteria.andLoIdcardLike(str);
            logistics = logisticsMapper.selectByExample(ex);
        }
        for (Logistics logistic : logistics) {
            System.out.println(logistic);
        }
        System.out.println(logistics);
        return logistics;
    }
}
